package com.zwg.mode.mvp

import android.text.TextUtils

/**
 * Author wengang
 * Time 2024/5/26
 *
 * Presenter 层
 * 主要负责逻辑处理
 */
class LoginPresenter {

    //常量
    companion object {
        const val STATE_LOGIN_LOADING = 0
        const val STATE_LOGIN_SUCCESS = 1
        const val STATE_LOGIN_FAILED = 2
    }

    //持有了usermodel  mvc架构
    private val userModel by lazy {
        MVPUserModel()
    }

    fun checkUserNameState(accout: String, callback: MVPLoginActivity) {
        userModel.checkUserState(accout) {
            when (it) {
                0 -> {
                    callback.onExist()
                }

                1 -> {
                    callback.onNotExist()
                }
            }
        }

    }


    interface OnCheckUserNameStateResultCallback {
        fun onNotExist()
        fun onExist()

    }

    fun doLogin(accout: String, password: String, callback: OnDoLoginStateChange) {

        //检查账号是否正确
        if (TextUtils.isEmpty(accout)) {
            //TODO 提示账号有问题
            callback.onAccountFormatError()
            return
        }
        //检查密码是否正确
        if (TextUtils.isEmpty(password)) {
            //TODO 密码有问题
            callback.onPasswordEmpty()
            return

        }

        userModel.doLogin(accout, password) {
            when (it) {
                STATE_LOGIN_LOADING -> {
                    callback.onLoading()
                }

                STATE_LOGIN_SUCCESS -> {
                    callback.onLoginSuccess()
                }

                STATE_LOGIN_FAILED -> {
                    callback.onLoginFailed()
                }
            }
        }

    }

    interface OnDoLoginStateChange {
        fun onAccountFormatError()
        fun onPasswordEmpty()
        fun onLoading()

        fun onLoginSuccess()

        fun onLoginFailed()
    }

}